package com.zl.lib_base.base

import android.app.Activity
import android.content.Context
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.zl.lib_base.anno.FragmentConfiguration

/**
 * author : wutao
 * time   : 2020/10/14
 * desc   :
 * version: 1.1
 */
abstract class BaseFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLayoutId) {

    private var useEventBus = false
    protected lateinit var activity: Activity
    private var time: Long = 0
    private var oldMsg: String? = null

    init {
        this.javaClass.getAnnotation(FragmentConfiguration::class.java)?.let {
            useEventBus = it.useEventBus
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        activity = requireActivity()
    }

    /**
     * 相同msg 只显示一个。
     */
    fun showToast(msg: String) {
        if (msg != oldMsg) {
            Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
            time = System.currentTimeMillis()
        } else {
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
                time = System.currentTimeMillis()
            }
        }
        oldMsg = msg
    }
}